package com.softer.chengxin.score.controller;

import com.softer.chengxin.common.domin.Common;
import com.softer.chengxin.common.domin.CommonResult;
import com.softer.chengxin.common.domin.ManagerPageResult;
import com.softer.chengxin.common.domin.Page;
import com.softer.chengxin.common.enums.ResultEnum;
import com.softer.chengxin.score.entity.Appeal;
import com.softer.chengxin.score.entity.ScoreDTO;
import com.softer.chengxin.score.entity.ScoreDetail;
import com.softer.chengxin.score.service.ScoreDetailService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author Gong Zhiyang
 * @date 2018/9/27
 */
@RestController
@RequestMapping("/api/ScoreDetail")
public class ScoreDetailController {
      @Autowired
    private ScoreDetailService scoreDetailService;

    /**
     * 保存信息录入。
     *
     * @param scoreDetail 数据实体
     * @return 提交结果
     */
    @PostMapping("/AddScoreDetail")
    public CommonResult addScoreDetail(ScoreDetail scoreDetail, HttpSession session) {
        // 查询企业ID
        Integer id = (Integer) session.getAttribute("ID");
        String loginName = (String) session.getAttribute("loginName");
        if (StringUtils.isBlank(loginName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "请重新登陆后操作");
        }
        if (StringUtils.isBlank(scoreDetail.getTime())) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "请选择发文时间");
        }
        scoreDetail.setAdder(loginName);
        scoreDetail.setState(Integer.parseInt(Common.ZERO));
        if (StringUtils.isBlank(scoreDetail.getProjectId())) {
            scoreDetail.setProjectId(Common.ZERO);
        }
        return scoreDetailService.addScoreDetail(scoreDetail, id);
    }

    /**
     * 信息录入,直接提交。
     *
     * @param scoreDetail 数据实体
     * @return 提交结果
     */
    @PostMapping("/submitScoreDetail")
    public CommonResult submitScoreDetail(ScoreDetail scoreDetail, HttpSession session) {
        // 查询企业ID
        Integer id = (Integer) session.getAttribute("ID");
        //  登录名
        String loginName = (String) session.getAttribute("loginName");
        if (StringUtils.isBlank(loginName)) {
            return new CommonResult(ResultEnum.FAIL.getCode(), "请重新登陆后操作");
        }
        scoreDetail.setAdder(loginName);
        scoreDetail.setState(Integer.parseInt(Common.ONE));
        if (StringUtils.isBlank(scoreDetail.getProjectId())) {
            scoreDetail.setProjectId(Common.ZERO);
        }
        return scoreDetailService.addScoreDetail(scoreDetail, id);
    }

    /**
     * 先保存后，提交信息录入，只需要修改状态
     *
     * @param id    得分详情ID
     * @param state 状态
     * @return 处理结果
     */
    @PostMapping("/updateState")
    public CommonResult updateState(Integer id, Integer state) {
        return scoreDetailService.updateState(id, state);
    }

    /**
     * 获取录入信息详情
     *
     * @param id 录入信息Id
     * @return 信息详情
     */
    @PostMapping("/getScoreDetail")
    public ScoreDetail getScoreDetail(Integer id) {
        return scoreDetailService.getScoreDetail(id);
    }

    /**
     * 删除录入信息
     *
     * @param id 录入信息ID
     * @return 处理结果
     */
    @PostMapping("/delete")
    public CommonResult delete(Integer id) {
        return scoreDetailService.delete(id);
    }

    /**
     * 查询录入信息列表
     *
     * @param page     页面实体
     * @param scoreDTO 查询实体
     * @return 列表
     */
    @PostMapping("/query")
    public ManagerPageResult query(Page page, ScoreDTO scoreDTO, HttpSession session) {
        // 查询企业ID
        Integer id = (Integer) session.getAttribute("ID");
        // 登录名
        String loginName = (String) session.getAttribute("loginName");
        if (StringUtils.isBlank(loginName)) {
            return new ManagerPageResult();
        }
        scoreDTO.setCorpId(id);
        //获取项目ID
        String projectId = (String) session.getAttribute("projectId");
        if (StringUtils.isBlank(projectId)) {
            projectId = Common.ZERO;
        }
        scoreDTO.setProjectId(projectId);
        return scoreDetailService.query(page, scoreDTO, loginName);
    }

    /**
     * 企业申诉
     *
     * @param appeal 申诉实体
     * @return 结果
     */
    @PostMapping("/appeal")
    public CommonResult appeal(Appeal appeal) {
        return scoreDetailService.appeal(appeal);
    }

    /**
     * 获取最近的信息（用于首页展示）
     *
     * @return 信息集合
     */
    @PostMapping("/getCurrentMsg")
    public List<ScoreDetail> getCurrentMsg(HttpSession session) {
        // 查询企业ID
        Integer id = (Integer) session.getAttribute("ID");
        if (id == null) {
            return new ArrayList<>();
        }
        //获取项目ID
        String projectId = (String) session.getAttribute("projectId");
        if (StringUtils.isBlank(projectId)) {
            projectId = Common.ZERO;
        }
        return scoreDetailService.getCurrentMsg(id, projectId);
    }

    /**
     * 编辑录入信息
     *
     * @param scoreDetail 录入信息实体
     * @return 录入结果
     */
    @PostMapping("/edit")
    public CommonResult edit(ScoreDetail scoreDetail) {
        if (StringUtils.isBlank(scoreDetail.getProjectId())) {
            scoreDetail.setProjectId(Common.ZERO);
        }
        return scoreDetailService.edit(scoreDetail);
    }

    /**
     * 查询综合查询的年度集合
     *
     * @return 年度集合
     */
    @PostMapping("/getYearList")
    public List<String> getYearList() {
        return scoreDetailService.getYearList();
    }
}
